let fun={
    login:()=>{
        let phone=document.getElementById("phone").value;
        let code=document.getElementById("code").value;
        let command=window.localStorage.getItem("command");
        console.log(command);
        if(command===null || command===undefined || command.length===0){
            if(!fun.testPhone(phone))return;
            if(!fun.testCode(code))return;
            command="";
        }else code="";
        fetch(window.server_path+window.server_post.login,{
            method:"POST",
            headers:{"Content-Type":"application/json"},
            body:JSON.stringify({
                phone:phone,
                code:code,
                command:command,
                timestamp:new Date().getTime()
            })
        }).then(e=>{return e.json();}).then(data=>{
            if(data.status){
                window.crlog.success(data.tip);
                if(data.command!=null && data.command.length>0)window.localStorage.setItem("command",data.command);
                window.sessionStorage.setItem("phone",data.phone);
                window.location.href="manage";
            }else{
                window.crlog.error(data.tip);
                window.localStorage.clear();
            }
        })
    },
    testPhone:(phone)=>{
        if(phone===null || phone===undefined || phone.length===0){
            window.crlog.warning("请输入手机号");
            return false;
        }
        if(phone.length!==11 || !/^1[3456789][0-9]+/.test(phone)){
            window.crlog.warning("手机号格式不对");
            return false;
        }
        return true;
    },
    testCode:(code)=>{
        if(code===null || code===undefined || code.length===0){
            window.crlog.warning("请输入验证码");
            return false;
        }
        if(code.length!==6){
            window.crlog.warning("验证码格式错误");
            return false;
        }
        return true;
    },
    sendVerificationCode:()=>{
        let phone=document.getElementById("phone").value;
        if(!fun.testPhone(phone))return;
        fetch(window.server_path+window.server_post.sendVerificationCode,{
            method:"POST",
            headers:{"Content-Type":"application/json"},
            body:JSON.stringify({
                phone:phone
            })
        }).then(e=>{return e.json();}).then(data=>{
            if(data.status){
                console.log(data);
                window.crlog.success("验证码发送成功");
                let bu=document.getElementById("sendCode");
                bu.onclick=()=>{};
                fun.countTime(bu,parseInt(data.past_due));
            }else window.crlog.error(data.tip);
        })
    },
    /**
     * 倒计时
     * @param bu {HTMLElement}
     * @param time {Number}
     */
    countTime:(bu,time)=>{
        bu.innerText=time+"";
        time--;
        if(time>0)setTimeout(fun.countTime,1000,bu,time);
        else{
            bu.innerText="获取验证码";
            bu.onclick=()=>{fun.sendVerificationCode()};
        }
    }
}